This code covers chapter 5 of “Introduction to Data Mining” by Pang-Ning Tan, Michael Steinbach and Vipin Kumar.

CC This work is licensed under the Creative Commons Attribution 4.0 International License. For questions please contact Michael Hahsler.

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.2     ✓ dplyr   1.0.6
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## 
## Attaching package: 'arules'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)

Load the iris dataset

data(iris)
summary(iris)
##   Sepal.Length    Sepal.Width     Petal.Length    Petal.Width   
##  Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100  
##  1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300  
##  Median :5.800   Median :3.000   Median :4.350   Median :1.300  
##  Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199  
##  3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800  
##  Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500  
##        Species  
##  setosa    :50  
##  versicolor:50  
##  virginica :50  
##                 
##                 
## 

Convert the data to transactions. Note that the features are numeric and need to be discretized. The conversion automatically applies frequency-based discretization with 3 classes to each numeric feature (with a warning).

iris_trans <- transactions(iris)
## Warning: Column(s) 1, 2, 3, 4 not logical or factor. Applying default
## discretization (see '? discretizeDF').
inspect(head(iris_trans))
##     items                     transactionID
## [1] {Sepal.Length=[4.3,5.4),               
##      Sepal.Width=[3.2,4.4],                
##      Petal.Length=[1,2.63),                
##      Petal.Width=[0.1,0.867),              
##      Species=setosa}                      1
## [2] {Sepal.Length=[4.3,5.4),               
##      Sepal.Width=[2.9,3.2),                
##      Petal.Length=[1,2.63),                
##      Petal.Width=[0.1,0.867),              
##      Species=setosa}                      2
## [3] {Sepal.Length=[4.3,5.4),               
##      Sepal.Width=[3.2,4.4],                
##      Petal.Length=[1,2.63),                
##      Petal.Width=[0.1,0.867),              
##      Species=setosa}                      3
## [4] {Sepal.Length=[4.3,5.4),               
##      Sepal.Width=[2.9,3.2),                
##      Petal.Length=[1,2.63),                
##      Petal.Width=[0.1,0.867),              
##      Species=setosa}                      4
## [5] {Sepal.Length=[4.3,5.4),               
##      Sepal.Width=[3.2,4.4],                
##      Petal.Length=[1,2.63),                
##      Petal.Width=[0.1,0.867),              
##      Species=setosa}                      5
## [6] {Sepal.Length=[5.4,6.3),               
##      Sepal.Width=[3.2,4.4],                
##      Petal.Length=[1,2.63),                
##      Petal.Width=[0.1,0.867),              
##      Species=setosa}                      6

Mine Association Rules

rules <- apriori(iris_trans, parameter = list(support = 0.1, confidence = 0.8))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.8    0.1    1 none FALSE            TRUE       5     0.1      1
##  maxlen target  ext
##      10  rules TRUE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 15 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[15 item(s), 150 transaction(s)] done [0.00s].
## sorting and recoding items ... [15 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 5 done [0.00s].
## writing ... [144 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
rules
## set of 144 rules

Interactive inspect with sorting, filtering and paging

inspectDT(rules)

Scatter plot

Plot rules as a scatter plot using an interactive html widget. To avoid overplotting, jitter is added automatically. Set jitter = 0 to disable jitter. Hovering over rules shows rule information. Note: plotly/javascript does not do well with too many points, so plot selects the top 1000 rules with a warning if more rules are supplied.

plot(rules, engine = "html")
## To reduce overplotting, jitter is added! Use jitter = 0 to prevent jitter.

Matrix

Plot rules as a matrix using an interactive html widget.

plot(rules, method = "matrix", engine = "html") 

Graph

Plot rules as a graph using an interactive html widget. Note: the used javascript library does not do well with too many graph nodes, so plot selects the top 100 rules only (with a warning).

plot(rules, method = "graph", engine = "html")
## Warning: Too many rules supplied. Only plotting the best 100 rules using lift
## (change control parameter max if needed)

Interactive Rule Explorer

You can specify a rule set or a dataset. To explore rules that can be mined from iris, use: ruleExplorer(iris)

The rule explorer creates an interactive Shiny application that can be used locally or deployed on a server for sharing. A deployed version of the ruleExplorer is available here (using shinyapps.io).